package com.tarena.tocketmq.producer;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;

import java.util.ArrayList;
import java.util.List;

/**
 * 批量消息
 */
public class BatchProducer {
    public static void main(String[] args) {
        DefaultMQProducer producer = new DefaultMQProducer("batch-producer-group");
        try {
            producer.start();

            //If you just send messages of no more than 1MiB at a time, it is easy to use batch
            //Messages of the same batch should have: same topic, same waitStoreMsgOK and no schedule support
            String topic = "BatchMessage";
            List<Message> messages = new ArrayList<>();
            messages.add(new Message(topic, "Tag", "OrderID001", "Hello world 0".getBytes()));
            messages.add(new Message(topic, "Tag", "OrderID002", "Hello world 1".getBytes()));
            messages.add(new Message(topic, "Tag", "OrderID003", "Hello world 2".getBytes()));
            producer.send(messages);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            producer.shutdown();
        }

    }
}
